home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / deluge / bencode.py < prev    next >
Text File  |  2009-06-16  |  3KB  |  130 lines

  1. # The contents of this file are subject to the Python Software Foundation
  2. # License Version 2.3 (the License).  You may not copy or use this file, in
  3. # either source code or executable form, except in compliance with the License.
  4. # You may obtain a copy of the License at http://www.python.org/license.
  5. #
  6. # Software distributed under the License is distributed on an AS IS basis,
  7. # WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
  8. # for the specific language governing rights and limitations under the
  9. # License.
  10.  
  11. # Written by Petru Paler
  12.  
  13. # Minor modifications made by Andrew Resch to replace the BTFailure errors with Exceptions
  14.  
  15. def decode_int(x, f):
  16.     f += 1
  17.     newf = x.index('e', f)
  18.     n = int(x[f:newf])
  19.     if x[f] == '-':
  20.         if x[f + 1] == '0':
  21.             raise ValueError
  22.     elif x[f] == '0' and newf != f+1:
  23.         raise ValueError
  24.     return (n, newf+1)
  25.  
  26. def decode_string(x, f):
  27.     colon = x.index(':', f)
  28.     n = int(x[f:colon])
  29.     if x[f] == '0' and colon != f+1:
  30.         raise ValueError
  31.     colon += 1
  32.     return (x[colon:colon+n], colon+n)
  33.  
  34. def decode_list(x, f):
  35.     r, f = [], f+1
  36.     while x[f] != 'e':
  37.         v, f = decode_func[x[f]](x, f)
  38.         r.append(v)
  39.     return (r, f + 1)
  40.  
  41. def decode_dict(x, f):
  42.     r, f = {}, f+1
  43.     while x[f] != 'e':
  44.         k, f = decode_string(x, f)
  45.         r[k], f = decode_func[x[f]](x, f)
  46.     return (r, f + 1)
  47.  
  48. decode_func = {}
  49. decode_func['l'] = decode_list
  50. decode_func['d'] = decode_dict
  51. decode_func['i'] = decode_int
  52. decode_func['0'] = decode_string
  53. decode_func['1'] = decode_string
  54. decode_func['2'] = decode_string
  55. decode_func['3'] = decode_string
  56. decode_func['4'] = decode_string
  57. decode_func['5'] = decode_string
  58. decode_func['6'] = decode_string
  59. decode_func['7'] = decode_string
  60. decode_func['8'] = decode_string
  61. decode_func['9'] = decode_string
  62.  
  63. def bdecode(x):
  64.     try:
  65.         r, l = decode_func[x[0]](x, 0)
  66.     except (IndexError, KeyError, ValueError):
  67.         raise Exception("not a valid bencoded string")
  68.  
  69.     return r
  70.  
  71. from types import StringType, IntType, LongType, DictType, ListType, TupleType
  72.  
  73.  
  74. class Bencached(object):
  75.  
  76.     __slots__ = ['bencoded']
  77.  
  78.     def __init__(self, s):
  79.         self.bencoded = s
  80.  
  81. def encode_bencached(x,r):
  82.     r.append(x.bencoded)
  83.  
  84. def encode_int(x, r):
  85.     r.extend(('i', str(x), 'e'))
  86.  
  87. def encode_bool(x, r):
  88.     if x:
  89.         encode_int(1, r)
  90.     else:
  91.         encode_int(0, r)
  92.  
  93. def encode_string(x, r):
  94.     r.extend((str(len(x)), ':', x))
  95.  
  96. def encode_list(x, r):
  97.     r.append('l')
  98.     for i in x:
  99.         encode_func[type(i)](i, r)
  100.     r.append('e')
  101.  
  102. def encode_dict(x,r):
  103.     r.append('d')
  104.     ilist = x.items()
  105.     ilist.sort()
  106.     for k, v in ilist:
  107.         r.extend((str(len(k)), ':', k))
  108.         encode_func[type(v)](v, r)
  109.     r.append('e')
  110.  
  111. encode_func = {}
  112. encode_func[Bencached] = encode_bencached
  113. encode_func[IntType] = encode_int
  114. encode_func[LongType] = encode_int
  115. encode_func[StringType] = encode_string
  116. encode_func[ListType] = encode_list
  117. encode_func[TupleType] = encode_list
  118. encode_func[DictType] = encode_dict
  119.  
  120. try:
  121.     from types import BooleanType
  122.     encode_func[BooleanType] = encode_bool
  123. except ImportError:
  124.     pass
  125.  
  126. def bencode(x):
  127.     r = []
  128.     encode_func[type(x)](x, r)
  129.     return ''.join(r)
  130.